package me.landery.modules.sinco.util;

import me.landery.modules.sinco.domain.Mode;

import java.util.Calendar;
import java.util.Date;

public class CalculateUtil {
    /**
     * 计算套餐需要的价格
     * @param mode 套餐
     * @return 价格
     */
    public   static Double calculatePrice(Mode mode ){
        if (mode.getIsdiscount() == 0){ //不打折，那么需要的费用就是价格
            return mode.getPrice();
        }

        if (mode.getIsdiscount() == 1){ //按照百分比打折 0.1就是1折
            Double discount = mode.getDiscount();
            if (discount > 1){ //大于1就是1折
                return mode.getPrice();
            }
            if (discount <= 0){ //免费
                return 0.0;
            }
            return mode.getPrice()*discount;
        }
        if(mode.getIsdiscount() == 2){
            Double discount = mode.getDiscount();
            if (discount > mode.getPrice()){ //优惠力度超过就是0元
                return 0.0;
            }
            return mode.getPrice()- discount;
        }

        return mode.getPrice();
    }

    /**
     * 两日期之间的间隔
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @return 返回day
     */
    public static int getDistanceByDay(Date beginDate, Date endDate ) {
        Calendar beginCalendar = Calendar.getInstance();
        beginCalendar.setTime(beginDate);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);

        long beginTime=beginCalendar.getTimeInMillis();
        long endTime=endCalendar.getTimeInMillis();
        //先算出两时间的毫秒数之差大于一天的天数
        int betweenDays = (int)(Math.abs(endTime - beginTime) / (1000 * 60 * 60 *24));
        //使endCalendar减去这些天数，将问题转换为两时间的毫秒数之差不足一天的情况
        endCalendar.add(Calendar.DAY_OF_MONTH, -betweenDays);
        //再使endCalendar减去1天
        endCalendar.add(Calendar.DAY_OF_MONTH, -1);
        //比较两日期的DAY_OF_MONTH是否相等
        if(beginCalendar.get(Calendar.DAY_OF_MONTH)==endCalendar.get(Calendar.DAY_OF_MONTH)){
            return betweenDays + 1;    //相等说明确实跨天了
        } else{
            return betweenDays;    //不相等说明确实未跨天
        }
    }

}
